package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"
	"net/url"
)

func handler(w http.ResponseWriter, r *http.Request) {
	proxyURL, err := url.Parse("http://proxy.example.com:8888") // 你要使用的代理服务器地址和端口

	// 创建一个ReverseProxy
	proxy := httputil.NewSingleHostReverseProxy(proxyURL)

	// 修改请求头部等
	r.URL.Host = proxyURL.Host
	r.URL.Scheme = proxyURL.Scheme
	r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
	r.Host = proxyURL.Host

	// 发起反向代理
	proxy.ServeHTTP(w, r)
}

func main() {
	http.HandleFunc("/", handler)
	fmt.Println("Proxy server running on :8080")
	http.ListenAndServe(":8080", nil)
}
